<!-- 手签协议 -->
<template>
  <div>
    <c-title text="门店审核"></c-title>
    <van-tabs v-model="activeName" @click="handleClick" sticky :ellipsis="false" swipeable offset-top="40">
      <van-tab :title="items.name" v-for="(items, ind) in titleArr" :key="ind">
        <div class="list" v-if="dataList[ind] && !fun.isTextEmpty(dataList[ind].list)">
          <div class="list-child margin-top-10" v-for="(item, index) in dataList[ind].list" :key="index">
            <van-cell-group inset class="t-l" @click="toUrl('BusDistStApDet',{apply_id: item.id})">
              <!-- status : 0未审核，1审核通过，2驳回 -->
              <van-cell class="f-12" :title="item.created_at" :value="`${item.status == 1 ? '审核通过' : item.status == 2 ? '已驳回' : '待审核'}`" :border="false" />
              <van-cell class="f-16" :title="item.store_name" value="" :border="false">
                <template #label>
                  <div class="flex"><img :src="item.avatar" class="min-img" />{{ item.nickname }}</div>
                </template>
              </van-cell>
              <div class="flex" style="padding: 0 10px 10px;flex-direction: row-reverse;" v-if="item.status == 0">
                <van-button round size="small" style="width: 80px;" color="var(--themeBaseColor)" @click="submitStauts(1,item.id)">通过</van-button>
                <van-button round plain hairline size="small" style="width: 80px;margin-right: 12px;" color="var(--themeBaseColor)" @click="submitStauts(0,item.id)">驳回</van-button>
              </div>
            </van-cell-group>
          </div>
        </div>
        <div v-else style="color: #999;padding:1rem;" class="t-c">没有更多~</div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      activeName: 0,
      dataList: [],
      titleArr: [],
      isLoadMore: true,

      loading: false
    };
  },

  activated() {},
  created() {
    this.init();
  },

  components: {},

  computed: {},

  mounted() {},

  methods: {
    toUrl(name, params = {}, query = {}) {
      this.$router.push(this.fun.getUrl(name, params, query));
    },
    init() {
      this.dataList = [];
      this.titleArr = [
        { id: "", name: "全部" },
        { id: 0, name: "待审核" },
        { id: 1, name: "审核通过" }
      ];
      this.isLoadMore = true;
      this.setNewArr();
    },
    setNewArr() {
      this.titleArr.forEach((val, index) => {
        this.dataList.push({
          isLoadMore: true,
          loading: false,
          finished: false,
          page: 1,
          total_page: 0,
          list: []
        });
      });
      this.handleClick();
    },
    onSearch() {
      this.dataList[this.activeName].list = [];
      this.dataList[this.activeName].page = 1;
      this.handleClick();
    },
    handleClick() {
      if (!this.fun.isTextEmpty(this.dataList[this.activeName].list)) {
        this.isLoadMore = this.dataList[this.activeName].isLoadMore;
        return;
      }
      $http
        .get("plugin.business-district.frontend.controllers.promoter.store-apply", { page: 1, status: this.titleArr[this.activeName].id }, ".")
        .then(response => {
          if (response.result === 1) {
            this.isLoadMore = true;
            this.dataList[this.activeName].isLoadMore = true;
            this.dataList[this.activeName].page = 1;
            this.dataList[this.activeName].total_page = response.data.last_page;
            this.$set(this.dataList[this.activeName], "list", response.data.data);
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      that.dataList[that.activeName].isLoadMore = false;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (that.dataList[that.activeName].page >= that.dataList[that.activeName].total_page) {
        return;
      } else {
        let _json = {
          status: this.titleArr[this.activeName].id
        };
        that.dataList[that.activeName].page = that.dataList[that.activeName].page + 1;
        _json.page = that.dataList[that.activeName].page;

        $http.get("plugin.business-district.frontend.controllers.promoter.store-apply", _json, "加载中").then(
          response => {
            that.isLoadMore = true;
            that.dataList[that.activeName].isLoadMore = true;
            if (response.result === 1) {
              var myData = response.data.data;
              that.dataList[that.activeName].list = that.dataList[that.activeName].list.concat(myData); //数组拼接
            } else {
              that.dataList[that.activeName].page = that.dataList[that.activeName].page - 1;
              that.dataList[that.activeName].isLoadMore = false;
              that.isLoadMore = false;
            }
          },
          function(response) {
            // error callback
          }
        );
      }
    },
    submitStauts(status,id) {
      this.$dialog
        .confirm({
          message: `是否确认${status == 1 ? "通过" : "驳回"}该门店申请？`
        })
        .then(() => {
          this.submitApply(status,id);
        })
        .catch(() => {
          // on cancel
        });
    },
    async submitApply(status,id){
      this.isLock = true;
      if(!this.isLock) return;
      let { result, msg, data } = await $http.get('plugin.business-district.frontend.controllers.promoter.apply',{apply: status,apply_id: id},".");
      this.isLock = false;
      if(result == 0) return this.$toast(msg);
      this.onSearch();
    }
  }
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
.min-img {
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  margin: 0 0.25rem 0 0;
}
::v-deep .van-tabs__line {
  background: var(--themeBaseColor);
  bottom: 20px;
  width: 20px;
}

.pop-base-class {
  .pop-base-title {
    margin: 18px 12px 12px;
    font-size: 1rem;
    font-weight: bold;
    text-align: left;
  }
  .pop-base-content {
    min-height: 45vh;
    max-height: 55vh;
    overflow: scroll;
    padding: 0 0 5rem;
  }
  .pop-base-bottom {
    padding: 0.625rem 0.875rem;
    background: #ffffff;
  }
}
</style>
